package com.lyn.module_custom_photo.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

import com.lyn.module_custom_photo.entity.ImageModel;

import java.util.ArrayList;
import java.util.List;

/**
 * created by lyn at 2021/10/27
 */
public class PhotoUtil {


    private static class PhotoUtilTypeClass {
        private static PhotoUtil instance = new PhotoUtil();
    }

    public static PhotoUtil getInstance() {
        return PhotoUtilTypeClass.instance;
    }

    /**
     * 获取照片路径
     * @param context
     * @return
     */
    public  List<ImageModel> getImages(Context context){
        List<ImageModel> list = new ArrayList<ImageModel>();
        ContentResolver contentResolver = context.getContentResolver();
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String[] projection = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA,};
        String sortOrder = MediaStore.Images.Media.DATE_ADDED + " desc";
        Cursor cursor = contentResolver.query(uri, projection, null, null, sortOrder);
        int iId = cursor.getColumnIndex(MediaStore.Images.Media._ID);
        int iPath = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            String id = cursor.getString(iId);
            String path = cursor.getString(iPath);
            ImageModel imageModel = new ImageModel(id,path);
            list.add(imageModel);
            cursor.moveToNext();
        }
        cursor.close();
        return list;
    }


    /**
     * 根据路径从硬盘中读取图片
     * @param path 图片路径
     * @param reqWidth 请求宽度（显示宽度）
     * @param reqHeight 请求高度（显示高度）
     * @return 图片Bitmap
     */
    public Bitmap decodeBitmapFromDisk(String path, int reqWidth, int reqHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        //初始压缩比例
        options.inSampleSize = calculateBitmapSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        Bitmap bmp = BitmapFactory.decodeFile(path, options);

        return bmp;
    }

    /**
     * 计算压缩率
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static int calculateBitmapSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }
}
